import trim from 'lodash.trim'

/**
 * <a-button v-if="$auth('test:[ADD,DELETE]')">Button</a-button>
 * @param Vue
 */

function plugin (Vue) {
    if (plugin.installed) {
        return
    }

    !Vue.prototype.$auth && Object.defineProperties(Vue.prototype, {
        $auth: {
            get () {
                const _this = this
                return (permissions) => {
                    const [permission, actionString] = permissions.split(':')
                    // 去除两边的 '[', ']'
                    const actions = actionString.replace(/(\[)|(\])/g, '').split(',')
                    const list = _this.$store.state.permission.permissions[permission] || []
                    return actions.every(action => {
                        return !trim(action) ? true : list.includes(trim(action))
                    })
                }
            }
        }
    })
}

export default plugin
